package service

import (
	"context"
	__ "user-server/basic/proto"
	"user-server/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {
	return &__.RegisterResp{
		Code: 0,
		Msg:  "",
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) MemberPackage(_ context.Context, in *__.MemberPackageReq) (*__.MemberPackageResp, error) {

	memberPackage := model.MemberPackage{
		Name:     in.Name,
		Price:    float64(in.Price),
		Duration: in.Duration,
	}

	if err := memberPackage.CreateMemberPackage(); err != nil {
		return &__.MemberPackageResp{
			Code: 400,
			Msg:  "视频会员创建失败",
		}, nil
	}

	return &__.MemberPackageResp{
		Code: 200,
		Msg:  "视频会员创建成功",
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) RechargeOrder(_ context.Context, in *__.RechargeOrderReq) (*__.RechargeOrderResp, error) {

	order := model.RechargeOrder{
		UserId:    int(in.UserId),
		PackageId: int(in.PackageId),
		Amount:    float64(in.Amount),
		PayStatus: int(in.PayStatus),
	}

	if err := order.CreateRechargeOrder(); err != nil {
		return &__.RechargeOrderResp{
			Code: 400,
			Msg:  "会员充值订单创建失败",
		}, nil
	}

	return &__.RechargeOrderResp{
		Code: 200,
		Msg:  "会员充值订单创建成功",
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) ViewingPermission(_ context.Context, in *__.ViewingPermissionReq) (*__.ViewingPermissionResp, error) {

	permission := model.ViewingPermission{
		UserId:    int(in.UserId),
		PackageId: int(in.PackageId),
		Status:    int(in.Status),
	}

	if err := permission.CreateViewingPermission(); err != nil {
		return &__.ViewingPermissionResp{
			Code: 400,
			Msg:  "会员权限激活失败",
		}, nil
	}

	return &__.ViewingPermissionResp{
		Code: 200,
		Msg:  "会员权限激活成功",
	}, nil
}
